package com.hen.web.henweb.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hen.web.henweb.exception.BusinessException;
import com.hen.web.henweb.mapper.UserMapper;
import com.hen.web.henweb.model.po.User;
import com.hen.web.henweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 狠穿
 * @version 1.0.0
 * @date 2023/5/1
 * @function 用户实体类
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据ID查询用户
     * @param userId 用户ID
     * @return User
     */
    @Override
    public User findUserById(String userId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,userId);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 用户登录
     * @param account 账号
     * @param password 密码
     * @return User
     */
    @Override
    public User userLogin(String account, String password) {
        if(StrUtil.isBlank(account) || StrUtil.isBlank(password)){
            throw new BusinessException("账号和密码不可为空!");
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount,account);
        queryWrapper.eq(User::getPassword,password);
        User user = userMapper.selectOne(queryWrapper);
        if(user == null){
            throw new BusinessException("账号或密码错误!");
        }
        return user;
    }
}
